/**
 * Created by python on 18-10-4.
 */

function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}



$(function() {
    $("#change_password").submit(function (e) {
        e.preventDefault()

        // TODO 修改密码提交
        var params = {};
        // 获取到所有的参数
        $(this).serializeArray().map(function (x) {
            params[x.name] = x.value;
        });
        // 取到参数以便判断
        var old_password = params["old_password"];
        var new_password = params["new_password"];
        var new_password2 = params["new_password2"];

        // 两次密码不一致。报错
        if (new_password != new_password2) {
            alert("两次密码不一致")
            return
        }

        params = {
            "new_password": new_password,
            "old_password": old_password
        }

        $.ajax({
            url: "/admin/change_password",
            type: "post",
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            data: JSON.stringify(params),
            success: function (resp) {
                if (resp.errno == "0") {
                    // 返回上一页，刷新数据
                    alert("添加成功")
                    location.reload();
                } else {
                    alert(resp.errmsg);
                }
            }
        })
    })
})